package com.ruoyi.StarStudio.Controller;

import com.ruoyi.StarStudio.Utils.QrCodeUtils;
import com.ruoyi.common.core.redis.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.swing.filechooser.FileSystemView;
import java.io.File;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/qrcode")
public class QrCodeController {

    @Autowired
    private RedisCache redisCache;

    //生成并返回顾客小程序码
    @PostMapping(value = "/customer")
    public ResponseEntity<byte[]> getCustomerQrCode(String sceneStr) {
        File desktopDir = FileSystemView.getFileSystemView().getHomeDirectory();
        String desktopPath = desktopDir.getAbsolutePath();
        String filepath = desktopPath + sceneStr + "Customer.jpeg";  //在此更改小程序码需要保存的路径
        //需要跳转到的小程序的页面
        String page = "pages/route/scansource/scansource";  //果树信息展示页面

        String accessToken = null;
        accessToken = redisCache.getCacheObject("wxAccessToken");
        if (accessToken == null){
            accessToken = QrCodeUtils.getAccessToken();  //获取accessToken
            redisCache.setCacheObject("wxAccessToken", accessToken, 7200, TimeUnit.SECONDS);
        }

        return QrCodeUtils.getQrCode(sceneStr, accessToken, filepath, page);
    }

    //生成并返回果农小程序码
    @PostMapping(value = "/farmer")
    public ResponseEntity<byte[]> getFarmerQrCode(String sceneStr) {
        File desktopDir = FileSystemView.getFileSystemView().getHomeDirectory();
        String desktopPath = desktopDir.getAbsolutePath();
        String filepath = desktopPath + sceneStr + "Farmer.jpeg";  //在此更改小程序码需要保存的路径
        //需要跳转到的小程序的页面
        String page = "pages/scanchange/index";  //果农修改果树信息页面

        String accessToken = null;
        accessToken = redisCache.getCacheObject("wxAccessToken");
        if (accessToken == null){
            accessToken = QrCodeUtils.getAccessToken();  //获取accessToken
            redisCache.setCacheObject("wxAccessToken", accessToken, 7200, TimeUnit.SECONDS);
        }

        return QrCodeUtils.getQrCode(sceneStr, accessToken, filepath, page);
    }

}
